# -*- coding: utf-8 -*-

import cv2
import matplotlib.pyplot as plt
import numpy as np
import math

img_bgr = cv2.imread('imgs/lena.jpg')
color = ('b', 'g', 'r')
for i, col in enumerate(color):
    histr = cv2.calcHist([img_bgr], [i], None, [256], [0, 256])
    plt.plot(histr, color=col)
    plt.xlim([0, 256])
plt.title('Histogram of BGR image')
plt.savefig('imgs/hist_bgr.png')

img_gray = cv2.imread('imgs/lena.jpg', 0)
equ = cv2.equalizeHist(img_gray)
cv2.compareHist()
res = np.hstack((img_gray, equ))  # stacking images side-by-side
cv2.imwrite('imgs/gray.png', res)

hist_img_gray = cv2.calcHist([img_gray], [0], None, [256], [0, 256])
hist_equ = cv2.calcHist([equ], [0], None, [256], [0, 256])
fig = plt.figure()
plt.subplot(1, 2, 1)
plt.hist(hist_img_gray, bins=50)
plt.title('Histogram of gray image')
plt.subplot(1, 2, 2)
plt.hist(hist_equ, bins=50)
plt.title('Histogram of gray image(equalized)')
fig.savefig('imgs/hist_gray.png')
fig.clear()

# ================== 图像规定化 ============================================
r = np.asarray([i for i in range(256)])
sigma = 30
miu = 120
p = 1. / (np.sqrt(2. * math.pi) * sigma) * np.exp(-(np.square(r - miu)) / (2. * sigma ** 2.))  # 单峰高斯函数的表达式
plt.plot(r, p)
plt.title('Gaussian')
fig.savefig('imgs/p.png')
fig.clear()

map_add_gray = np.zeros((256,))
temp = 0.
for i in range(256):
    temp += p[i]
    map_add_gray[i] = temp

print temp

temp = 0.
for i in range(256):
    if p[i] >= 0.0001:
        temp += 1
print temp

point = 0
reflect_p = np.random.randint(1, 10, [temp, ])
reflect_gray = np.random.randint(1, 10, [temp, ])
for i in range(256):
    if p[i] >= 0.0001:
        reflect_gray[point] = i - 1
        reflect_p[point] = map_add_gray[i]
        point += 1

x = reflect_p.shape[0]
y = reflect_p.shape[1]
reflect_p[y] = 1

## ====================================== SML
compare_result = np.zeros((temp,))
final_gray = np.zeros((256,))
point = 1
for i in range(256):
    for j in range(temp):
        compare_result[j] = (abs(hist_img_gray[i] - reflect_p[j]))

    [X, Y] = np.min(compare_result)
    final_gray[i] = reflect_gray[Y]

gray1 = np.zeros((256, 256))
for i in range(256):
    for j in range(256):
        gray1[i, j] = final_gray[img_gray[i, j] + 1]

gray1 = np.asarray(gray1, np.uint8)

plt.imshow(gray1)
plt.title('SML')
fig.savefig('imgs/SML.png')
fig.clear()

# ==================================== GML
compare_result2 = np.zeros((1, 256))
final_gray2 = np.zeros((1, 256))
temp1 = 1
temp3 = 1
temp2 = 1
for i in range(temp):
    for j in range(256):
        compare_result2[j] = abs(reflect_p[i] - gray1[j])
    if i == temp:
        Y = 256
    if temp2 == Y:
        temp1 = temp3

    temp3 = temp1
    temp2 = Y
    temp1 = Y + 1

gray2 = np.zeros((256, 256))
for i in range(256):
    for j in range(256):
        gray2[i, j] = final_gray2[gray1[i, j] + 1]

plt.imshow(gray2)
plt.title('GML映射后的图像')
numofgray = np.zeros((1, 256))
for i in range(256):
    for j in range(256):
        numofgray[gray1[i, j] + 1] = numofgray[gray1[i, j] + 1] + 1

plt.hist(gray1)
plt.title('SML直方图')

plt.hist(gray2)
plt.title('GML直方图')
